/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.resource.service;

import com.lczyfz.istep.common.constants.DeleteFlag;
import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.common.service.CrudService;
import com.lczyfz.istep.modules.resource.dao.ResourceCtfSetDao;
import com.lczyfz.istep.modules.resource.dao.ResourceCtfSetSubjectDao;
import com.lczyfz.istep.modules.resource.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * CTF题目集管理Service
 * @author dh
 * @version 2017-01-16
 */
@Service
@Transactional(readOnly = true)
public class ResourceCtfSetSubjectService extends CrudService<ResourceCtfSetSubjectDao, ResourceCtfSetSubject> {

	@Autowired
	ResourceCtfSetDao resourceCtfSetDao;

	@Autowired
	ResourceCtfSetService resourceCtfSetService;

	@Autowired
	ResourceCtfSubjectService resourceCtfSubjectService;

//fd
	public ResourceCtfSetSubject get(String id) {
		return super.get(id);
	}
	
	public List<ResourceCtfSetSubject> findList(ResourceCtfSetSubject resourceCtfSetSubject) {
		return super.findList(resourceCtfSetSubject);
	}
	
	public Page<ResourceCtfSetSubject> findPage(Page<ResourceCtfSetSubject> page, ResourceCtfSetSubject resourceCtfSetSubject) {
		return super.findPage(page, resourceCtfSetSubject);
	}


	
	@Transactional(readOnly = false)
	public void save(String duallist,ResourceCtfSet resourceCtfSet) {
		String[] resourceCtfSubjectList = new String[0];
		if (duallist != null && !duallist.equals(""))
			resourceCtfSubjectList = duallist.split(",");
		logger.debug("123456");
		logger.debug("length: [{}]", resourceCtfSubjectList);
//		resourcePaper.setClassHour(resourceCoursewareList.length+"");
		resourceCtfSet.setSubjectSum(resourceCtfSubjectList.length+"");
		int count=0;
		for(int i=0; i<resourceCtfSubjectList.length;i++)
		{
			ResourceCtfSubject resourceCtfSubject = resourceCtfSubjectService.get(resourceCtfSubjectList[i]);
			if(resourceCtfSubject.getCtfEnvironmentId()!=null && !resourceCtfSubject.getCtfEnvironmentId().equals(""))
			{
				count++;
			}
		}
		resourceCtfSet.setExperimentSum(count + "");

		resourceCtfSetService.save(resourceCtfSet);

		for (int i = 0; i < resourceCtfSubjectList.length; i++) {

			ResourceCtfSetSubject resourceCtfSetSubject=new ResourceCtfSetSubject();
			resourceCtfSetSubject.setCtfSetId(resourceCtfSet.getId());
			resourceCtfSetSubject.setCtfSubjectId(resourceCtfSubjectList[i]);

			super.save(resourceCtfSetSubject);
		}


	}
	
	@Transactional(readOnly = false)
	public void delete(ResourceCtfSetSubject resourceCtfSetSubject) {
		super.delete(resourceCtfSetSubject);
	}



//	public Page<ResourceCtfSet> findPageByTrainprogramId(Page<ResourceCtfSet> page, ResourceCtfSet resourceCtfSet) {
//		resourceCtfSet.setPage(page);
//		page.setList(dao.findPageByTrainprogramId(resourceCtfSet));
//		return page;
//	}
//
//	//我的CTF-CTF题目集列表展示
//	public Page<ResourceCtfSet> stuCtfSetList(Page<ResourceCtfSet> page, ResourceCtfSet resourceCtfSet) {
//		resourceCtfSet.setPage(page);
//		page.setList(resourceCtfSetDao.stuCtfSetList(resourceCtfSet));
//		return page;
//	}
//
//	//我的学习任务-获取CTF题目集名称
//	public List<ResourceCtfSet> stuLearnCtfSet(String distributeId, String userId){
//		return resourceCtfSetDao.stuLearnCtfSet(distributeId, userId, DeleteFlag.deleteFlag);
//	}
	@Transactional(readOnly = false)
	public void update(String duallist,ResourceCtfSet resourceCtfSet) {
		ResourceCtfSetSubject temp=new ResourceCtfSetSubject();
//		temp.setPaperId(resourcePaper.getId());
		temp.setCtfSetId(resourceCtfSet.getId());
		List<ResourceCtfSetSubject> resourceCtfSetSubjectList=super.findList(temp);
		logger.debug("Ctf题目集-CTF题目中间表数量: [{}]", resourceCtfSetSubjectList.size());

		String[] resourceCtfSubjectList = new String[0];
		if (!"".equals(duallist))
			resourceCtfSubjectList = duallist.split(",");
		logger.debug("length: [{}]", resourceCtfSubjectList.length);
		resourceCtfSet.setSubjectSum(resourceCtfSubjectList.length+"");
		int count=0;
		for(int i=0; i<resourceCtfSubjectList.length;i++)
		{
			ResourceCtfSubject resourceCtfSubject = resourceCtfSubjectService.get(resourceCtfSubjectList[i]);
			if(resourceCtfSubject.getCtfEnvironmentId()!=null && !resourceCtfSubject.getCtfEnvironmentId().equals(""))
			{
				count++;
			}
		}
		resourceCtfSet.setExperimentSum(count + "");

		resourceCtfSetService.save(resourceCtfSet);

		for (int i = 0; i < resourceCtfSetSubjectList.size(); i++) {
			super.delete(resourceCtfSetSubjectList.get(i));
		}
		for (int i = 0; i < resourceCtfSubjectList.length; i++) {

			ResourceCtfSetSubject resourceCtfSetSubject=new ResourceCtfSetSubject();
			resourceCtfSetSubject.setCtfSetId(resourceCtfSet.getId());
			resourceCtfSetSubject.setCtfSubjectId(resourceCtfSubjectList[i]);
			super.save(resourceCtfSetSubject);
		}
	}

}